<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
                <label class="el-form-item-label">工单号</label>
                <el-input v-model="query.workOrderCode" clearable placeholder="工单号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
                <label class="el-form-item-label">产品型号</label>
                <el-input v-model="query.productCode" clearable placeholder="产品型号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker
          v-model="query.createTime"
          start-placeholder="createTimeStart"
          end-placeholder="createTimeStart"
          class="date-item"
        />
        <date-range-picker
          v-model="query.updateTime"
          start-placeholder="updateTimeStart"
          end-placeholder="updateTimeStart"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="90px">
          <!--<el-form-item label="id" prop="id">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>-->
          <el-form-item label="开始序列号" prop="startSerial">
            <el-input-number v-model="form.startSerial" style="width: 300px" :step="1" step-strictly></el-input-number>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              type="textarea"
              :rows="2"
              v-model="form.remark"
              style="width: 300px;"
              placeholder="请输入跳码备注
例如：
跳码序列号：500
操作人：小帅
操作时间：2023年5月23日10:39:27"/>
          </el-form-item>
          <el-form-item label="前序列号">
            <el-input v-model="form.firstSerial" style="width: 300px;" disabled="true"/>
          </el-form-item>
          <el-form-item label="下个序列号">
            <el-input v-model="form.lastSerial" style="width: 300px;" disabled="true"/>
          </el-form-item>
          <el-form-item label="工单号">
            <el-input v-model="form.workOrderCode" style="width: 300px;" disabled="true"/>
          </el-form-item>
          <el-form-item label="产品型号">
            <el-input v-model="form.productCode" style="width: 300px;" disabled="true"/>
          </el-form-item>
         <!-- <el-form-item label="工单状态">
            <el-input v-model="form.orderStatus" style="width: 370px;" />
          </el-form-item>-->
          <el-form-item label="创建人">
            <el-input v-model="form.createBy" style="width: 300px;" disabled="true"/>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="form.createTime" type="datetime" style="width: 300px;" disabled="true"/>
          </el-form-item>
          <el-form-item label="更新人">
            <el-input v-model="form.updateBy" style="width: 300px;" disabled="true"/>
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker v-model="form.updateTime" type="datetime" style="width: 300px;" disabled="true"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
<!--        <el-table-column prop="id" label="id" />-->
        <el-table-column prop="startSerial" label="开始序列号" />
        <el-table-column prop="firstSerial" label="前序列号" />
        <el-table-column prop="lastSerial" label="下个序列号" />
        <el-table-column prop="totalSerialCount" label="生成总数量" />
        <el-table-column prop="workOrderCode" label="工单号" />
        <el-table-column prop="productCode" label="产品型号" />
        <el-table-column prop="remark" label="备注" />
<!--        <el-table-column prop="orderStatus" label="工单状态" />-->
        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateBy" label="更新人" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column v-if="checkPer(['admin','orderSerial:edit','orderSerial:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudOrderSerial from '@/api/product/orderSerial'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/U.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, startSerial: null, firstSerial: null, lastSerial: null, workOrderCode: null, productCode: null, remark: null, orderStatus: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'OrderSerial',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD(
      { title: '工单序列号',
        url: 'api/orderSerial',
        idField: 'id', sort: 'id,desc',
        optShow: {
          add: false,
          edit: false,
          del: false,
          download: false,
          reset: true,
          import: false
        },
        crudMethod: { ...crudOrderSerial }})
  },
  data() {
    var checkStartSerial = (rule, value, callback) => {
      if (value <= this.form.firstSerial) {
        callback(new Error('ERROR:跳码值必须大于当前产品序列号'+this.form.firstSerial));
      } else {
        callback();
      }
    };
    return {
      permission: {
        add: ['admin', 'orderSerial:add'],
        edit: ['admin', 'orderSerial:edit'],
        del: ['admin', 'orderSerial:del']
      },
      rules: {
        id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        startSerial:[
          { validator: checkStartSerial, trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'workOrderCode', display_name: '工单号' },
        { key: 'productCode', display_name: '产品型号' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
